package SmartHome.dao.impl;

import SmartHome.entity.RecordInfo;
import SmartHome.dao.RecordDao;
import SmartHome.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class RecordDaoImpl implements RecordDao{

    public int executeUpdate(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            // 1.加载驱动
            // 2.连接数据库
            conn = JdbcUtil.getConn();
            // 3.创建语句
            ps = conn.prepareStatement(sql);
            // 遍历参数
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            // 4.执行语句
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, ps, null);
        }
        return 0;
        }

    @Override
    public RecordInfo querydevice(int deviceId){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
       // String sql = "select recordId,recordTime,r.deviceType,Temperature,Humidity,Brightness,Open from recordinfo r,deviceinfo d where r."+?=d.?";
         try {
             String sql = "select * from recordinfo where deviceId ="+deviceId;
             conn = JdbcUtil.getConn();
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            if (rs.next()) {
                RecordInfo recordInfo = new RecordInfo((rs.getInt(1)), rs.getString("recordTime"),
                        rs.getString("deviceType"), rs.getInt("Temperature"),rs.getInt("Humidity"),
                        rs.getInt("Brightness"),rs.getInt("Open"),deviceId);
                System.out.println(recordInfo);
                return recordInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, st, rs);
        }
        return null;
    }

    @Override
    public RecordInfo querydevicetype(String devicetype){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        String sql = "select * from RecordInfo , where deviceType =" +"devicetype";
        try {
            conn = JdbcUtil.getConn();
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            if (rs.next()) {
                RecordInfo recordInfo = new RecordInfo((rs.getInt(1)), rs.getString("recordTime"),
                        rs.getString("deviceType"), rs.getInt("Temperature"),rs.getInt("Humidity"),
                        rs.getInt("Brightness"),rs.getInt("Open"),rs.getInt(8));
                System.out.println(recordInfo);
                return recordInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, st, rs);
        }
        return null;
    }

    @Override
    public List<RecordInfo> getAll() {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            st = conn.createStatement();
            String sql = "select * from RecordInfo ";
            System.out.println(sql);
            rs = st.executeQuery(sql);
            List<RecordInfo> list = new ArrayList<RecordInfo>();

            while(rs.next()) {
                RecordInfo recordInfo = new RecordInfo((rs.getInt(1)), rs.getString("recordTime"),
                        rs.getString("deviceType"), rs.getInt("Temperature"),rs.getInt("Humidity"),
                        rs.getInt("Brightness"),rs.getInt("Open"),rs.getInt("deviceId"));
                list.add(recordInfo);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, st, rs);
        }
        return null;
    }
    public int count(int temperature){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        String sql = "select COUNT(recordId) from RecordInfo as myCount, where Temperature="+temperature;
        try {
            // 1.加载驱动
            // 2.连接数据库
            conn = JdbcUtil.getConn();
            // 3.创建语句
            st = conn.createStatement();
            //ps.setInt(1, uId);
            // 4.执行语句
            rs = st.executeQuery(sql);
            if (rs.next()) {
                int count2=rs.getInt(1);
                return count2;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, st, rs);
        }
        return 0;

    }

}



